﻿#region Using directives
using System;
using System.ComponentModel;
using System.Linq;
using System.Web.Script.Services;
using System.Web.Services;

#endregion

namespace ProcolServices {
    /// <summary>
    /// Summary description for UserTeamService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class UserTeamService : WebService {
        private readonly ProcolDataContext db = new ProcolDataContext();

        [WebMethod]
        /// <summary>
        /// Adds a new user into a team.
        /// Return values:
        ///  -2 - Missing field(s)
        /// Anything else - team's UID in which the user were added
        /// </summary>
        /// 
        public int AddNewUserTeam(int teamId, int userId, int roleId) {
            var values = new Object[] {teamId, userId, roleId};
            foreach (var value in values) {
                String str = value.ToString();
                int tryParse;

                if (Int32.TryParse(str, out tryParse)) {
                    if (tryParse < 0)
                        return -2;
                } else if (String.IsNullOrEmpty(str))
                    return -2;
            }
            try {
                var newUserTeam = new UserTeam {TeamId = teamId, UserId = userId, RoleId = roleId};
                db.UserTeams.InsertOnSubmit(newUserTeam);
                db.SubmitChanges();

                return newUserTeam.TeamId;
            } catch (InvalidOperationException) {
                return -2;
            }
        }

        [WebMethod]
        /// <summary>
        /// Adds a new user into a team by entering its username.
        /// Return values:
        ///  -2 - Missing field(s)
        /// Anything else - team's UID in which the user were added
        /// </summary>
        /// 
        public int AddNewUserTeam(String teamName, String username, String roleName) {
            var values = new Object[] {teamName, username, roleName};
            foreach (var value in values) {
                String str = value.ToString();
                int tryParse;

                if (Int32.TryParse(str, out tryParse)) {
                    if (tryParse < 0)
                        return -2;
                } else if (String.IsNullOrEmpty(str))
                    return -2;
            }
            try {
                Team teamQuery = (from t in db.Teams
                                  where t.Name == teamName
                                  select t).Single();

                User userQuery = (from u in db.Users
                                  where u.Username == username
                                  select u).Single();

                TeamRole teamRole = (from tr in db.TeamRoles
                                     where tr.Name == roleName
                                     select tr).Single();

                int teamId = teamQuery.TeamId;
                int userId = userQuery.UserId;
                int roleId = teamRole.RoleId;


                var newUserTeam = new UserTeam {TeamId = teamId, UserId = userId, RoleId = roleId};
                db.UserTeams.InsertOnSubmit(newUserTeam);
                db.SubmitChanges();

                return newUserTeam.TeamId;
            } catch (InvalidOperationException) {
                return -2;
            }
        }

        [WebMethod]
        /// <summary>
        /// Update a registry on the table Userteam
        /// Return values:
        ///  -2 - Missing field(s)
        ///  -1 - Unsuccessfull update
        ///  -1 - Means that the update were successfull
        /// </summary>
        /// 
        public int updateUserTeamRoleId(String username, String teamName, String roleName) {
            var values = new Object[] {username, teamName, roleName};
            foreach (var value in values) {
                String str = value.ToString();
                int tryParse;

                if (Int32.TryParse(str, out tryParse)) {
                    if (tryParse < 0)
                        return -2;
                } else if (String.IsNullOrEmpty(str))
                    return -2;
            }
            try {
                Team teamQuery = (from t in db.Teams
                                  where t.Name == teamName
                                  select t).Single();

                User userQuery = (from u in db.Users
                                  where u.Username == username
                                  select u).Single();

                TeamRole teamRole = (from tr in db.TeamRoles
                                     where tr.Name == roleName
                                     select tr).Single();

                int teamId = teamQuery.TeamId;
                int userId = userQuery.UserId;
                int roleId = teamRole.RoleId;

                var userTeam = (from ut in db.UserTeams
                                where ut.TeamId == teamId && ut.UserId == userId
                                select ut).Single();

                userTeam.RoleId = roleId;
                return 1;
            } catch (InvalidOperationException) {
                return -1;
            }
        }

        [WebMethod]
        //Remove a user from a team , using their names
        // Return :
        // -2 - When field missing
        // -1 - When remove is unsuccessfull
        //  1 - When remove is successfull 
        public int removeUserFromTeam(String username, String teamName) {
            var values = new Object[] {username, teamName};
            foreach (var value in values) {
                String str = value.ToString();
                int tryParse;

                if (Int32.TryParse(str, out tryParse)) {
                    if (tryParse < 0)
                        return -2;
                } else if (String.IsNullOrEmpty(str))
                    return -2;
            }
            try {
                Team teamQuery = (from t in db.Teams
                                  where t.Name == teamName
                                  select t).Single();

                User userQuery = (from u in db.Users
                                  where u.Username == username
                                  select u).Single();

                int teamId = teamQuery.TeamId;
                int userId = userQuery.UserId;

                var userTeam = (from ut in db.UserTeams
                                where ut.TeamId == teamId && ut.UserId == userId
                                select ut).Single();

                db.UserTeams.DeleteOnSubmit(userTeam);
                db.SubmitChanges();

                return 1;
            } catch (InvalidOperationException) {
                return -1;
            }
        }
    }
}